
#include <WiFi.h>
#include <ESPmDNS.h>
#include <WiFiUdp.h>
#include <ArduinoOTA.h>
#include "lcd_touch_drv.h"

const char* ssid = "CMCC-xxxx";
const char* password = "xxxxxxxx";

/***************************************************************/
/***************************************************************/
void my_print(const char* dsc, uint32_t value)
{
  Serial.printf("%s->%d\r\n", dsc, value);
  Serial.flush();
}

/***************************************************************/
/***************************************************************/
void arduino_ota_setup() {
  Serial.println("Booting");
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);
  while (WiFi.waitForConnectResult() != WL_CONNECTED) {
    Serial.println("Connection Failed! Rebooting...");
    delay(5000);   ESP.restart();
  }
  ArduinoOTA.setHostname("lcd32");
  ArduinoOTA
  .onStart([]() {
    String type;
    if (ArduinoOTA.getCommand() == U_FLASH)
      type = "sketch";
    else // U_SPIFFS
      type = "filesystem";
  })
  .onEnd([]() {
    Serial.println("\nEnd");
  })
  .onProgress([](unsigned int progress, unsigned int total) {
    Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
  })
  .onError([](ota_error_t error) {
    Serial.printf("Error[%u]: ", error);
    if (error == OTA_AUTH_ERROR) Serial.println("Auth Failed");
    else if (error == OTA_BEGIN_ERROR) Serial.println("Begin Failed");
    else if (error == OTA_CONNECT_ERROR) Serial.println("Connect Failed");
    else if (error == OTA_RECEIVE_ERROR) Serial.println("Receive Failed");
    else if (error == OTA_END_ERROR) Serial.println("End Failed");
  });
  ArduinoOTA.begin();
  Serial.println("Ready");
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());
}

/***************************************************************/
/***************************************************************/
static lv_obj_t * img1;
static void button_event_handler(lv_obj_t* obj, lv_event_t event)
{
  lv_anim_t a;
  lv_anim_init(&a);
  lv_anim_path_t path;
  lv_anim_path_init(&path);
  lv_anim_set_ready_cb(&a, NULL);

  switch (event)
  { case LV_EVENT_PRESSED:
      lv_anim_path_set_cb(&path, lv_anim_path_ease_in_out);
      lv_anim_set_exec_cb(&a, (lv_anim_exec_xcb_t) lv_img_set_zoom);
      lv_anim_set_path(&a, &path);
      lv_anim_set_time(&a, 500);
      lv_anim_set_delay(&a, 100);
      lv_anim_set_var(&a, img1);
      lv_anim_set_values(&a, 64, 256);
      lv_anim_start(&a);
      break;
    case LV_EVENT_LONG_PRESSED:
      lv_anim_path_set_cb(&path, lv_anim_path_overshoot);
      lv_anim_set_exec_cb(&a, (lv_anim_exec_xcb_t) lv_img_set_angle);
      lv_anim_set_path(&a, &path);
      lv_anim_set_time(&a, 500);
      lv_anim_set_delay(&a, 100);
      lv_anim_set_var(&a, img1);
      lv_anim_set_values(&a, 0, 3600);
      lv_anim_start(&a);
      break;
    default:
      break;
  }
}

void my_app_start()
{
  LV_IMG_DECLARE(jingjunv);
  img1 = lv_img_create(lv_scr_act(), NULL);
  lv_img_set_src(img1, &jingjunv);
  lv_obj_align(img1, NULL, LV_ALIGN_CENTER, 0, 0);

  static lv_anim_path_t path_overshoot;
  lv_anim_path_init(&path_overshoot);
  lv_anim_path_set_cb(&path_overshoot, lv_anim_path_overshoot);

  static lv_anim_path_t path_ease_in_out;
  lv_anim_path_init(&path_ease_in_out);
  lv_anim_path_set_cb(&path_ease_in_out, lv_anim_path_ease_in_out);

  /*Gum-like button*/
  static lv_style_t style_gum;
  lv_style_init(&style_gum);
  lv_style_set_transform_width(&style_gum, LV_STATE_PRESSED, 10);
  lv_style_set_transform_height(&style_gum, LV_STATE_PRESSED, -10);
  lv_style_set_value_letter_space(&style_gum, LV_STATE_PRESSED, 5);
  lv_style_set_transition_path(&style_gum, LV_STATE_DEFAULT, &path_overshoot);
  lv_style_set_transition_path(&style_gum, LV_STATE_PRESSED, &path_ease_in_out);
  lv_style_set_transition_time(&style_gum, LV_STATE_DEFAULT, 250);
  lv_style_set_transition_delay(&style_gum, LV_STATE_DEFAULT, 100);
  lv_style_set_transition_prop_1(&style_gum, LV_STATE_DEFAULT, LV_STYLE_TRANSFORM_WIDTH);
  lv_style_set_transition_prop_2(&style_gum, LV_STATE_DEFAULT, LV_STYLE_TRANSFORM_HEIGHT);
  lv_style_set_transition_prop_3(&style_gum, LV_STATE_DEFAULT, LV_STYLE_VALUE_LETTER_SPACE);

  lv_obj_t* btn1 = lv_btn_create(lv_scr_act(), NULL);
  lv_obj_align(btn1, NULL, LV_ALIGN_CENTER, 0, 80);
  lv_obj_add_style(btn1, LV_BTN_PART_MAIN, &style_gum);
  lv_obj_set_style_local_value_str(btn1, LV_BTN_PART_MAIN, LV_STATE_DEFAULT, "hello, world!");
  lv_obj_set_event_cb(btn1, button_event_handler);
}

/***************************************************************/
/***************************************************************/
void setup()
{
  Serial.begin(115200);
  arduino_ota_setup();
  lcd_touch_drv_log_cb(my_print);
  lcd_touch_drv_init();
  my_app_start();
}

/***************************************************************/
/***************************************************************/
void loop()
{
  ArduinoOTA.handle();
  lv_task_handler();
  delay(5);
}
